package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.AssessmentTechJszMapper;
import com.zhijian.medical.entity.po.AssessmentTechJsz;
import com.zhijian.medical.entity.vo.request.AssessmentTechJszSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.AssessmentTechJszSearchVo;
import com.zhijian.medical.entity.vo.response.AssessmentTechJszVo;
import com.zhijian.medical.service.AssessmentTechJszService;
import com.zhijian.util.AssertUtil;
import com.zhijian.util.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 技术组技术人员考核-技术组业务处理层
 * 
 * @author auto
 * @version: 1.0
 * @date 2024-11-14 15:21:21
 */
@Slf4j
@Service
public class AssessmentTechJszServiceImpl implements AssessmentTechJszService {

	@Autowired
    private AssessmentTechJszMapper mapper;

    @Override
    public AssessmentTechJsz selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public AssessmentTechJszVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(AssessmentTechJsz assessmentTechJsz) {
        Date now = new Date();
        assessmentTechJsz.setCreateTime(now);
        assessmentTechJsz.setUpdateTime(now);
        return mapper.insert(assessmentTechJsz) > 0 ? assessmentTechJsz.getId() : 0L;
    }

    @Override
    public Long insertSelective(AssessmentTechJsz assessmentTechJsz) {
        Date now = new Date();
        assessmentTechJsz.setCreateTime(now);
        assessmentTechJsz.setUpdateTime(now);
        return mapper.insertSelective(assessmentTechJsz) > 0 ? assessmentTechJsz.getId() : 0L;
    }

    @Override
    public boolean update(AssessmentTechJsz assessmentTechJsz) {
        Date now = new Date();
        assessmentTechJsz.setUpdateTime(now);
        return mapper.updateByPrimaryKey(assessmentTechJsz) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(AssessmentTechJsz record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(AssessmentTechJszSaveOrUpdateVo saveOrUpdateVo) {
        AssessmentTechJsz assessmentTechJsz = new AssessmentTechJsz();
        BeanUtils.copyProperties(saveOrUpdateVo, assessmentTechJsz);
        return this.insert(assessmentTechJsz) != 0;
    }

    @Override
    public boolean modify(AssessmentTechJszSaveOrUpdateVo saveOrUpdateVo) {
        AssessmentTechJsz old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        AssessmentTechJsz assessmentTechJsz = new AssessmentTechJsz();
        BeanUtils.copyProperties(saveOrUpdateVo, assessmentTechJsz);
        return this.update(assessmentTechJsz);
    }

    @Override
    public AssessmentTechJszVo selectOne(AssessmentTechJszSearchVo assessmentTechJszSearchVo) {
        return mapper.selectOne(assessmentTechJszSearchVo);
     }

    @Override
    public Integer count(AssessmentTechJszSearchVo assessmentTechJszSearchVo) {
        return mapper.count(assessmentTechJszSearchVo);
    }

    @Override
    public List<AssessmentTechJszVo> selectList(AssessmentTechJszSearchVo assessmentTechJszSearchVo) {
        return mapper.selectList(assessmentTechJszSearchVo);
    }

    @Override
    public Page<AssessmentTechJszVo> selectPage(AssessmentTechJszSearchVo assessmentTechJszSearchVo) {
        AssertUtil.check(Objects.isNull(assessmentTechJszSearchVo.getPageNum()) || Objects.isNull(assessmentTechJszSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(assessmentTechJszSearchVo);
        //分页
        Page<AssessmentTechJszVo> page = new Page<>(true, count, assessmentTechJszSearchVo.getPageNum(), assessmentTechJszSearchVo.getPageSize());
        assessmentTechJszSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<AssessmentTechJszVo> responseVos = this.selectList(assessmentTechJszSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<AssessmentTechJsz> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(AssessmentTechJszSearchVo assessmentTechJszSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(assessmentTechJszSearchVo);
    }
    
}